package cn.xfdzx.android.apps.shop.activity.ruzhu;


import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.io.IOException;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MoreAddressMapActivity;
import cn.xfdzx.android.apps.shop.adapter.SelectOneAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.LicenseImgBeen;
import cn.xfdzx.android.apps.shop.bean.OneBeen;
import cn.xfdzx.android.apps.shop.bean.ShopIn1Been;
import cn.xfdzx.android.apps.shop.bean.UploadImgBeen;
import cn.xfdzx.android.apps.shop.contract.ShopInContract;
import cn.xfdzx.android.apps.shop.presenter.ShopInPresenter;
import cn.xfdzx.android.apps.shop.util.GlideEngine;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.ShopCarPopupWindow;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.util.UtilsLog;
import cn.xfdzx.android.apps.shop.widget.WheelPicker;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

/**
 * 新发地商家入驻
 */
public class XFDShopInActivity extends BaseMvpActivity<ShopInPresenter> implements ShopInContract.View {

    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.iv_add_logo)
    ImageView ivAddLogo;
    @BindView(R.id.iv_saomiao)
    ImageView ivSaoMiao;
    @BindView(R.id.iv_other)
    ImageView ivOther;
    @BindView(R.id.iv_mentou)
    ImageView ivMentou;
    @BindView(R.id.iv_shop_in)
    ImageView ivShopIn;
    @BindView(R.id.tv_select_one)
    TextView tvSelectOne;
    @BindView(R.id.ett_shop_name)
    EditText ettShopName;
    @BindView(R.id.ett_registered)
    EditText ettRegistered;
    @BindView(R.id.ett_period_validity)
    EditText ettPeriodValidity;
    @BindView(R.id.ett_fuze_ren)
    EditText ettFuzeRen;
    @BindView(R.id.ett_address)
    EditText ettAddress;
    @BindView(R.id.ett_now_address)
    EditText ettNowAddress;
    @BindView(R.id.ett_the_main)
    EditText ettTheMain;
    @BindView(R.id.ett_introduce)
    EditText ettIntroduce;
    @BindView(R.id.ett_retail_shop_name)
    EditText ettRetailShopName;
    @BindView(R.id.ett_branch_shop_num)
    EditText ettBranchShopNum;//分店数量
    @BindView(R.id.tv_select_cooperation_term)
    TextView tvSelectCooperationTerm;//合作期限
    @BindView(R.id.ett_bd_num)
    EditText ettBdNum;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.ll_retail_shop_name)
    LinearLayout llRetailShopName;
    @BindView(R.id.tv_xfd_zhuying_class)
    TextView tvXfdZhuyingClass;
    @BindView(R.id.tv_xfd_zhuying_project)
    TextView tvXfdZhuyingProject;
    @BindView(R.id.tv_whole_address)
    TextView tvWholeAddress;
    @BindView(R.id.tv_retail_shop_name)
    TextView tvRetailShopNameLine;
    @BindView(R.id.tv_xfd_zhuying_class_line)
    TextView tvXfdZhuyingClassLine;
    @BindView(R.id.tv_xfd_zhuying_project_line)
    TextView tvXfdZhuyingProjectLine;
    protected Handler handler = new Handler();
    private RelativeLayout.LayoutParams item_bar;
    private Context mContext;
    private List<OneBeen.DataBean> oneList = new ArrayList();

    private ShopCarPopupWindow popupWindow;
    private SelectOneAdapter selectOneAdapter;
    //分店合作期限
    private ShopCarPopupWindow yearPopupWindow;
    private WheelPicker wheelPicker;
    private List<String> yearList = new ArrayList<>();
    private int contract_year = 0;//合作期限


    private boolean is_Saomiao = false;
    private boolean is_Other = false;
    private boolean is_Mentou = false;
    private boolean is_ShopIn = false;
    private String offline_shop_name, inv_code, store_master = "", store_qualifications_img, store_qualifications_name, store_logo = "", category_id, bd_content = "", front_door_photo, in_store_photo;
    private String store_category = "0";//所属分类0零售 1批发
    private String lng, lat;//


    private ShopIn1Been shopIn1Been;
    private Gson gson = new Gson();

    @Override
    public int getLayoutId() {
        return R.layout.activity_xfd_shop_in;
    }

    @Override
    public void initView() {
        initHigh();
        mContext = XFDShopInActivity.this;


        store_category = getIntent().getStringExtra("type");
        StatusBarUtil.setLightStatusBar(this, false, true);
        mPresenter = new ShopInPresenter();
        mPresenter.attachView(this);

        pwPW();

        popupWindow();
        if (store_category.equals("0")) {
            llState.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            rlTitle.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            tvNext.setBackgroundResource(R.drawable.dfb9129_bg_r8);
            llRetailShopName.setVisibility(View.VISIBLE);
            tvRetailShopNameLine.setVisibility(View.VISIBLE);
            tvXfdZhuyingClass.setVisibility(View.GONE);
            tvXfdZhuyingClassLine.setVisibility(View.GONE);
            tvXfdZhuyingProjectLine.setVisibility(View.GONE);
            tvSelectOne.setVisibility(View.GONE);
            tvXfdZhuyingProject.setVisibility(View.GONE);
            ettTheMain.setVisibility(View.GONE);
        } else {
            llRetailShopName.setVisibility(View.GONE);
            tvRetailShopNameLine.setVisibility(View.GONE);
            tvXfdZhuyingClass.setVisibility(View.VISIBLE);
            tvXfdZhuyingClassLine.setVisibility(View.VISIBLE);
            tvXfdZhuyingProjectLine.setVisibility(View.VISIBLE);
            tvSelectOne.setVisibility(View.VISIBLE);
            tvXfdZhuyingProject.setVisibility(View.VISIBLE);
            ettTheMain.setVisibility(View.VISIBLE);
            llState.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            rlTitle.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            tvNext.setBackgroundResource(R.drawable.d02c557_bg_r8);
        }
//回显
        setText();
    }

    private void setText() {
        if (null == UtilsData.getPreference(this, "ruzhu").getString("shopIn1")) {
            shopIn1Been = new ShopIn1Been();
        } else {
            RoundedCorners roundedCorners = new RoundedCorners(10);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            shopIn1Been = gson.fromJson(UtilsData.getPreference(this, "ruzhu").getString("shopIn1"), ShopIn1Been.class);

            ettRetailShopName.setText(shopIn1Been.offline_shop_name);
            ettShopName.setText(shopIn1Been.store_name);

            if (!shopIn1Been.store_qualifications_img.isEmpty()) {
                is_Saomiao = true;
                store_qualifications_img = shopIn1Been.store_qualifications_img;
                Glide.with(mContext).load(store_qualifications_img).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivSaoMiao);
            }

            ettAddress.setText(shopIn1Been.store_address);
            ettRegistered.setText(shopIn1Been.store_qualifications_num);
            store_qualifications_name = shopIn1Been.store_qualifications_name;
            ettPeriodValidity.setText(shopIn1Been.store_qualifications_expire);
            ettFuzeRen.setText(shopIn1Been.store_master);
            if (!shopIn1Been.store_logo.isEmpty()) {
                is_Saomiao = true;
                store_logo = shopIn1Been.store_logo;
                Glide.with(mContext).load(store_logo).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivAddLogo);
            }
            tvWholeAddress.setText(shopIn1Been.whole_address);
            ettNowAddress.setText(shopIn1Been.store_current_address);
            ettIntroduce.setText(shopIn1Been.intro);
            ettTheMain.setText(shopIn1Been.salecate);

            if (!shopIn1Been.front_door_photo.isEmpty()) {
                is_Mentou = true;
                front_door_photo = shopIn1Been.front_door_photo;
                Glide.with(mContext).load(front_door_photo).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivMentou);
            }
            if (!shopIn1Been.in_store_photo.isEmpty()) {
                is_ShopIn = true;
                in_store_photo = shopIn1Been.in_store_photo;
                Glide.with(mContext).load(in_store_photo).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivShopIn);
            }

            ettBdNum.setText(shopIn1Been.inv_code);
            lng = shopIn1Been.lng;
            lat = shopIn1Been.lat;
            ettBranchShopNum.setText(shopIn1Been.store_num + "");
            if (shopIn1Been.contract_year.isEmpty()) {
                contract_year = 0;
            } else {
                contract_year = Integer.parseInt(shopIn1Been.contract_year);
                if (contract_year != 0) {
                    tvSelectCooperationTerm.setText(yearList.get(contract_year - 1));
                }
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        shopIn1Been.offline_shop_name = ettRetailShopName.getText().toString().trim();
        shopIn1Been.store_name = ettShopName.getText().toString().trim();

        shopIn1Been.store_qualifications_img = store_qualifications_img;
        shopIn1Been.store_address = ettAddress.getText().toString().trim();
        shopIn1Been.store_qualifications_num = ettRegistered.getText().toString().trim();
        shopIn1Been.store_qualifications_name = store_qualifications_name;
        shopIn1Been.store_qualifications_expire = ettPeriodValidity.getText().toString().trim();
        shopIn1Been.store_master = ettFuzeRen.getText().toString().trim();
        shopIn1Been.store_logo = store_logo;
        shopIn1Been.whole_address = tvWholeAddress.getText().toString().trim();
        shopIn1Been.store_current_address = ettNowAddress.getText().toString().trim();
        shopIn1Been.intro = ettIntroduce.getText().toString().trim();
        shopIn1Been.salecate = ettTheMain.getText().toString().trim();
        shopIn1Been.front_door_photo = front_door_photo;
        shopIn1Been.in_store_photo = in_store_photo;
        shopIn1Been.inv_code = ettBdNum.getText().toString().trim();
        shopIn1Been.lng = lng;
        shopIn1Been.lat = lat;
        shopIn1Been.store_num = ettBranchShopNum.getText().toString().trim();
        shopIn1Been.contract_year = contract_year + "";
        UtilsData.getPreference(this, "ruzhu").put("shopIn1", gson.toJson(shopIn1Been));
    }

    private void popupWindow() {
        yearPopupWindow = new ShopCarPopupWindow(this, R.layout.pw_unit);
        wheelPicker = yearPopupWindow.getRootView().findViewById(R.id.up_unit);
        yearList.add("一年");
        yearList.add("两年");
        yearList.add("三年");
        wheelPicker.setDataList(yearList);

        yearPopupWindow.getRootView().findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yearPopupWindow.shareClose();
            }
        });

        yearPopupWindow.getRootView().findViewById(R.id.tv_queren).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                contract_year = wheelPicker.getCurrentPosition() + 1;
                tvSelectCooperationTerm.setText(yearList.get(wheelPicker.getCurrentPosition()));
                yearPopupWindow.shareClose();
            }
        });

    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void oneTypeSuccess(OneBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            oneList.clear();
            oneList.addAll(bean.getData());
            selectOneAdapter.notifyDataSetChanged();
            popupWindow.showPopupWindow();
        } else {
            Toast.makeText(mContext, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void oneLicenseImgSuccess(LicenseImgBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {

            ettShopName.setText(bean.getData().getName());
            ettRegistered.setText(bean.getData().getCertNo());
            ettPeriodValidity.setText(bean.getData().getValidTerm());
            ettFuzeRen.setText(bean.getData().getLegalRepName());
            ettAddress.setText(bean.getData().getAddress());
            ettTheMain.setText(bean.getData().getScope());
            store_qualifications_name = bean.getData().getName();


        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }


    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @OnClick({R.id.tv_select_cooperation_term, R.id.tv_whole_address, R.id.iv_back, R.id.iv_add_logo, R.id.iv_saomiao, R.id.iv_other, R.id.iv_mentou, R.id.iv_shop_in, R.id.tv_next, R.id.tv_select_one})
    public void Onclick(View view) {
        switch (view.getId()) {
            case R.id.tv_select_cooperation_term:
                yearPopupWindow.showPopupWindow();
                break;
            case R.id.tv_whole_address:
                //跳转到更多地址页面
                //跳转到选择收货地址
                Intent intent_moer_address = new Intent(this, MoreAddressMapActivity.class);
                startActivityForResult(intent_moer_address, START_CODE);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_add_logo:
                selectImage(ivAddLogo);
                break;
            case R.id.iv_saomiao:
                selectImage(ivSaoMiao);
                break;
            case R.id.iv_other:
                selectImage(ivOther);
                break;
            case R.id.iv_mentou:
                selectImage(ivMentou);
                break;
            case R.id.iv_shop_in:
                selectImage(ivShopIn);
                break;
            case R.id.tv_select_one:
                if (oneList.size() == 0) {
                    mPresenter.oneType("-1", store_category);
                } else {
                    popupWindow.showPopupWindow();
                }
                break;
            case R.id.tv_next:
                if (store_category.equals("0")) {
                    if (ettRetailShopName.getText().toString().trim().isEmpty()) {
                        Toast.makeText(mContext, "请输入店面名称", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }

                if (!is_Saomiao) {
                    Toast.makeText(mContext, "请上传营业执照", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ettShopName.getText().toString().trim().isEmpty()) {
                    Toast.makeText(mContext, "请输入注册公司名称", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ettRegistered.getText().toString().trim().isEmpty()) {
                    Toast.makeText(mContext, "请输入注册号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ettPeriodValidity.getText().toString().trim().isEmpty()) {
                    Toast.makeText(mContext, "请输入有效期", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ettFuzeRen.getText().toString().trim().isEmpty()) {
                    Toast.makeText(mContext, "请输入店铺所属人", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ettAddress.getText().toString().trim().isEmpty()) {
                    Toast.makeText(mContext, "请输入营业执照地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (tvWholeAddress.getText().toString().trim().equals("请选择地址")) {
                    Toast.makeText(mContext, "请选择店铺现在地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ettNowAddress.getText().toString().trim().isEmpty()) {
                    Toast.makeText(mContext, "请输入详细地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (store_category.equals("1")) {
                    if (tvSelectOne.getText().equals("请选择")) {
                        Toast.makeText(mContext, "请选择主营分类", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (ettTheMain.getText().toString().trim().isEmpty()) {
                        Toast.makeText(mContext, "请输入主营项目", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if (!is_Mentou) {
                    Toast.makeText(mContext, "请上传门头照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!is_ShopIn) {
                    Toast.makeText(mContext, "请上传店内照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ettBranchShopNum.getText().toString().trim().isEmpty()) {
                    Toast.makeText(mContext, "请输入分店数量", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (contract_year == 0) {
                    Toast.makeText(mContext, "请选择合作年限", Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent intent = new Intent(this, XFDShopInZhengActivity.class);
                intent.putExtra("offline_shop_name", ettRetailShopName.getText().toString().trim());
                intent.putExtra("store_name", ettShopName.getText().toString().trim());
                intent.putExtra("store_master", ettFuzeRen.getText().toString().trim());
                intent.putExtra("store_qualifications_img", store_qualifications_img);
                intent.putExtra("store_category", store_category);
                intent.putExtra("store_address", ettAddress.getText().toString().trim());
                intent.putExtra("store_qualifications_num", ettRegistered.getText().toString().trim());
                intent.putExtra("store_qualifications_name", store_qualifications_name);
                intent.putExtra("store_qualifications_expire", ettPeriodValidity.getText().toString().trim());
                intent.putExtra("store_logo", store_logo);
                intent.putExtra("category_id", category_id);
                intent.putExtra("whole_address", tvWholeAddress.getText().toString().trim());
                intent.putExtra("store_current_address", ettNowAddress.getText().toString().trim());
                if (ettIntroduce.getText().toString().trim().isEmpty()) {
                    intent.putExtra("intro", "新鲜好货，尽在掌握");
                } else {
                    intent.putExtra("intro", ettIntroduce.getText().toString().trim());
                }
                intent.putExtra("salecate", ettTheMain.getText().toString().trim());
                intent.putExtra("front_door_photo", front_door_photo);
                intent.putExtra("in_store_photo", in_store_photo);
                intent.putExtra("bd_content", bd_content);
                intent.putExtra("inv_code", ettBdNum.getText().toString().trim());
                intent.putExtra("lng", lng + "");
                intent.putExtra("lat", lat + "");
                intent.putExtra("store_num", ettBranchShopNum.getText().toString().trim());
                intent.putExtra("contract_year", contract_year + "");

                startActivity(intent);
                break;
        }
    }

    public void selectImage(ImageView imageView) {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage()).maxSelectNum(1)
                .loadImageEngine(GlideEngine.createGlideEngine())
                .minSelectNum(1).imageSpanCount(4)
                .selectionMode(PictureConfig.SINGLE)
                .compress(true)
                .minimumCompressSize(1000)
                .compressQuality(60)
                .isCamera(true)
                .previewImage(true)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        // onResult Callback
                        if (result.size() != 0) {
                            UtilsLog.error("dizhi", result.get(0).getCompressPath());
//                                    new File(result.get(0).getPath())
//                                    mPresenter.upload(new File(result.get(0).getPath()));
                            showLoading();
                            upLoadFiles(Constants.BASE_URL + "/addons/shimmer_liveshop/index.php/api/6/3/plugin/merch/common/apply/uploadImg", new File(result.get(0).getCompressPath()), new ConnectListener() {
                                @Override
                                public void connectSuccess(String result) {
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            hideLoading();
                                            try {
                                                Gson gson = new Gson();
                                                UploadImgBeen uploadImgBeen = gson.fromJson(result, UploadImgBeen.class);
                                                if (uploadImgBeen.getErrno().equals("200")) {

                                                    RoundedCorners roundedCorners = new RoundedCorners(10);
                                                    //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
                                                    RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                                                    Glide.with(mContext).load(uploadImgBeen.getData().getSrc()).apply(options.transform(new CenterCrop(), roundedCorners)).into(imageView);
                                                    if (imageView == ivAddLogo) {

                                                        store_logo = uploadImgBeen.getData().getSrc();
                                                    } else if (imageView == ivSaoMiao) {
                                                        is_Saomiao = true;
                                                        store_qualifications_img = uploadImgBeen.getData().getSrc();
                                                        mPresenter.licenseImg(uploadImgBeen.getData().getSrc());
                                                    } else if (imageView == ivOther) {
                                                        is_Saomiao = true;
                                                        bd_content = uploadImgBeen.getData().getSrc();
                                                    } else if (imageView == ivMentou) {
                                                        is_Mentou = true;
                                                        front_door_photo = uploadImgBeen.getData().getSrc();
                                                    } else if (imageView == ivShopIn) {
                                                        is_ShopIn = true;
                                                        in_store_photo = uploadImgBeen.getData().getSrc();
                                                    }

                                                } else {
                                                    Toast.makeText(mContext, uploadImgBeen.getMessage(), Toast.LENGTH_SHORT).show();
                                                }
                                            } catch (Exception e) {
                                                Toast.makeText(mContext, "上传失败", Toast.LENGTH_SHORT).show();

                                            }
                                        }
                                    });

                                }

                                @Override
                                public void connectFailed(String error) {
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            hideLoading();

                                        }
                                    });
                                }
                            });
                        }
                    }

                    @Override
                    public void onCancel() {
                        // onCancel Callback
                    }
                });
    }


    public void upLoadFiles(final String url, File file, final ConnectListener connectListener) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().proxy(Proxy.NO_PROXY).connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .readTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .writeTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .build();
        MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
        multipartBodyBuilder.setType(MultipartBody.FORM);
        //遍历map中所有参数到builder
        multipartBodyBuilder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
        //构建请求体
        RequestBody requestBody = multipartBodyBuilder.build();
        Request.Builder RequestBuilder = new Request.Builder();
        RequestBuilder.url(url);// 添加URL地址
        RequestBuilder.post(requestBody);
        RequestBuilder.addHeader("Authori-zation", "Bearer " + APP_TOKEN);
        Request request = RequestBuilder.build();

        final Call call = okHttpClient.newCall(request);
        //异步get请求
        call.enqueue(new Callback() {
                         @Override
                         public void onFailure(Call call, IOException e) {
                             connectListener.connectFailed(e.toString());
                             UtilsLog.error("错误", e.toString());
                         }

                         @Override
                         public void onResponse(Call call, Response response) throws IOException {
                             String result = response.body().string();
                             connectListener.connectSuccess(result);
                             UtilsLog.error("成功", result);
                         }
                     }
        );
    }


    public interface ConnectListener {
        void connectSuccess(String result);

        void connectFailed(String error);
    }

    private void pwPW() {
        if (popupWindow == null) {
            popupWindow = new ShopCarPopupWindow(this, R.layout.pw_select_one);
            popupWindow.getRootView().findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.shareClose();
                }
            });
            selectOneAdapter = new SelectOneAdapter(mContext, oneList);
            ((ListView) popupWindow.getRootView().findViewById(R.id.lv_select_one)).setAdapter(selectOneAdapter);
            ((ListView) popupWindow.getRootView().findViewById(R.id.lv_select_one)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    tvSelectOne.setText(oneList.get(position).getName());
                    category_id = oneList.get(position).getId() + "";
                    popupWindow.shareClose();
                }
            });
        }

    }

    @Override
    public void onBackPressed() {
        if (popupWindow != null) {
            if (popupWindow.isShowing()) {
                popupWindow.shareClose();
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        if (popupWindow != null) {
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
            popupWindow = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == START_CODE) {
                String addressName = data.getStringExtra("address_name");
                String district = data.getStringExtra("district");
                String city = data.getStringExtra("city");
                lng = data.getStringExtra("lontitude");
                lat = data.getStringExtra("latitude");

                if (null != addressName) {
                    tvWholeAddress.setText(city + district + addressName);
                    tvWholeAddress.setTextColor(getResources().getColor(R.color.color_333333));
                }
            }
        }
    }
}
